import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

/// Created by XieXin on 2020/10/10.
/// 加载对话框
class LoadingDialog {
  late BuildContext context;
  String loadText;
  bool isShow = false;
  Function? mState;

  LoadingDialog(this.context, {this.loadText = "加载中"});

  void showLoading() {
    if (isShow) return;
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      barrierColor: const Color(0x00000001),
      builder: (context) {
        isShow = true;
        return WillPopScope(
            //禁用返回键
            child: Center(
              child: Container(
                width: 200.w,
                height: 200.w,
                decoration: BoxDecoration(
                  color: const Color(0x5ecccccc),
                  borderRadius:
                      BorderRadius.circular(ScreenUtil().setWidth(16)),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SpinKitThreeBounce(
                      color: Theme.of(context).primaryColor,
                    ),
                    StatefulBuilder(
                      builder: (context, state) {
                        mState = state;
                        return Text(
                          loadText,
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(28),
                            color: Colors.white,
                          ),
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
            onWillPop: () async {
              isShow = false;
              return Future.value(false);
            });
      },
    );
  }

  void updateText(String text) {
    loadText = text;
    if (mState != null) mState!(() {});
  }

  void pop() {
    if (isShow) {
      isShow = false;
      Navigator.of(context).pop();
    }
  }
}
